home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!news
- From: jshoor@ix.netcom.com(Jonas Shoor )
- Newsgroups: comp.lang.c,comp.unix.programmer
- Subject: Problems execing passwd using pipes
- Date: 20 Mar 1996 14:00:49 GMT
- Organization: Netcom
- Message-ID: <4ip32h$srk@dfw-ixnews3.ix.netcom.com>
- NNTP-Posting-Host: ix-dc10-19.ix.netcom.com
- X-NETCOM-Date: Wed Mar 20 8:00:49 AM CST 1996
-
- I am trying to write a program to execute the passwd command.
-
- Passwd is interactive, so I set two pipes (one to read from the process
- and one to write to the process), and I fork off a process to execute
- the command.
-
- With this program, I can execute passwd, read its response (New
- passwd:), send the password string, but when I try to read the second
- response, it hangs. But if I hit the enter key the program will return
- "\nReenter new password:". I have tried to add "\n"s to the end
- of the passwd string, but it doesnt help.
-
- The code follows, it is modeled after examples in the "Using C on the
- UNIX System" O'Reilly book.
-
- Any ideas would be greatly appreciated, as I have exhausted all of
- mine.
-
- Thanks,
- Jonas
-
- #include<stdio.h>
-
- main()
- {
- FILE *fp1, *fp2;
- int pid, pipefds1[2], pipefds2[2];
- char username[14], username2[24];
-
- if (pipe(pipefds1) < 0){
- perror("pipe1");
- exit(1);
- }
- if (pipe(pipefds2) < 0){
- perror("pipe2");
- exit(1);
- }
-
- if ((pid=fork()) < 0){
- perror("fork");
- exit(1);
- }
- if (pid == 0){
- close(2); /* close stderr */
- dup(pipefds1[1]); /* set stderr to pipefds1[1] */
- close(pipefds1[1]);
- close(pipefds1[0]); /*close read side of pipefds1 */
- close(0); /* close stdin */
- dup(pipefds2[0]); /* set stdin to pipefds2[0] */
- close(pipefds2[0]);
- close(pipefds2[1]); /* close write side of pipefds2 */
- execl("/usr/bin/passwd", "passwd", "testvue", (char *) 0);
- perror("exec");
- exit(1);
- }
- close(pipefds1[1]); /* close write side of pipefds1 */
- close(pipefds2[0]); /* close read side of pipefds2 */
- fp1=fdopen(pipefds1[0],"r");
- fread(username, sizeof(char), 13, fp1); /* Read- New password: */
- username[13]='\0';
- printf("First Prompt = %s \n",username); /* Verify prompt */
-
- fp2=fdopen(pipefds2[1],"w"); /* Open pipefds2[1] for writing */
- fprintf(fp2, "aaaaa");
- fclose(fp2);
- printf("wFrom: jshoor@ix.netcom.com(Jonas Shoor )
- Newsgroups: comp.lang.c,comp.unix.programmer
- Subject: Problems execing passwd using pipes
-
- I am trying to write a program to execute the passwd command.
-
- Passwd is interactive, so I set two pipes (one to read from the process
- and one to write to the process), and I fork off a process to execute
- the command.
-
- With this program, I can execute passwd, read its response (New
- passwd:), send the password string, but when I try to read the second
- response, it hangs. But if I hit the enter key the program will return
- "\nReenter new password:". I have tried to add "\n"s to the end
- of the passwd string, but it doesnt help.
-
- The code follows, it is modeled after examples in the "Using C on the
- UNIX System" O'Reilly book.
-
- Any ideas would be greatly appreciated, as I have exhausted all of
- mine.
-
- Thanks,
- Jonas
-
- #include<stdio.h>
-
- main()
- {
- FILE *fp1, *fp2;
- int pid, pipefds1[2], pipefds2[2];
- char username[14], username2[24];
-
- if (pipe(pipefds1) < 0){
- perror("pipe1");
- exit(1);
- }
- if (pipe(pipefds2) < 0){
- perror("pipe2");
- exit(1);
- }
-
- if ((pid=fork()) < 0){
- perror("fork");
- exit(1);
- }
- if (pid == 0){
- close(2); /* close stderr */
- dup(pipefds1[1]); /* set stderr to pipefds1[1] */
- close(pipefds1[1]);
- close(pipefds1[0]); /*close read side of pipefds1 */
- close(0); /* close stdin */
- dup(pipefds2[0]); /* set stdin to pipefds2[0] */
- close(pipefds2[0]);
- close(pipefds2[1]); /* close write side of pipefds2 */
- execl("/usr/bin/passwd", "passwd", "testvue", (char *) 0);
- perror("exec");
- exit(1);
- }
- close(pipefds1[1]); /* close write side of pipefds1 */
- close(pipefds2[0]); /* close read side of pipefds2 */
- fp1=fdopen(pipefds1[0],"r");
- fread(username, sizeof(char), 13, fp1); /* Read- New password: */
- username[13]='\0';
- printf("First Prompt = %s \n",username); /* Verify prompt */
-
- fp2=fdopen(pipefds2[1],"w"); /* Open pipefds2[1] for writing */
- fprintf(fp2, "aaaaa");
- fclose(fp2);
- printf("writing to pipe2\n"); /* verify write */
-
- fread(username2, sizeof(char), 23, fp1); /* Read- Re-enter new
- password: */
- read(pipefds1[0],username2,23); /* It hangs HERE!! */
- fclose(fp1);
- username[23]='\0';
- printf("Second Prompt = %s \n",username2); /* Verify prompt */
- printf("End\n");
-
-
- /* Send answer matching answer to second prompt
- then wait for child process to finish */
-
- exit(0);
- }
-
-
- PS I am root when I execute this, and testvue does exist. Also, I am
- running on a sparc5 with Solaris 2.3
-